from selenium.common.exceptions import NoSuchElementException
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.user_product_computing_locator import ProductComputingLocator
from Case.python_ui.PageLocate.user_product_purchase_locator import UserProductPurchaseLocator
from selenium import webdriver
from Case.python_ui.PageLocate.user_product_waf_locator import ProductWafLocator
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import time


class UserProductPurchase(Basepage):

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 进入myresource页面
    def enter_my_resource_page(self):
        doc = '进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(UserProductPurchaseLocator.my_resource_menu, doc)
                self.click_element(UserProductPurchaseLocator.my_resource_menu, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    def get_resouce_name_to_click(self, resource_name):
        doc = '获取到资源名称然后点击'
        resource_list = self.get_elements(UserProductPurchaseLocator.resources, doc)
        for resource in resource_list:
            if resource.text == resource_name:
                resource.click()
        time.sleep(2)

    def get_prod_name_to_click(self, prod_name):
        doc = '获取产品名称然后点击'
        prod_list = self.get_elements(UserProductPurchaseLocator.prod_list, doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)

    # 进入My Resource中的Computing页
    def enter_computing_page(self):
        doc = "进入My Resource中的computing页"
        time.sleep(3)
        self.wait_elevisble(UserProductPurchaseLocator.security_page, doc)
        self.get_resouce_name_to_click("Computing")
        time.sleep(3)

    # 进入ECS购买页
    def enter_purchase_ecs_page(self):
        doc = "进入ECS购买页"
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.ecs_createnew_button, doc)
        self.move_mouse_to_element(UserProductPurchaseLocator.ecs_createnew_button)
        time.sleep(2)
        self.get_prod_name_to_click('Elastic Compute Service')
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.ecs_purchase_page, doc)
        # 验证ECS购买页面的相关数据字段
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_billing_methon_region_tittle, doc).text, 'Billing Method & Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_billing_methon_tittle, doc).text, 'Billing Method')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_region_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_zone_tittle, doc).text, 'Zone')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_organization_tittle, doc).text, 'Organization')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_instance_images_tittle, doc).text, 'Instance & Images')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_instance_name_tittle, doc).text, 'Instance Name')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_instance_type_tittle, doc).text, 'Instance Type')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_image_type_tittle, doc).text, 'Image Type')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_image_tittle, doc).text, 'Image')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_deployment_set_tittle, doc).text, 'Deployment Set')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_storage_tittle, doc).text, 'Storage')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_system_disk_tittle, doc).text, 'System Disk')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_snapshot_policy_tittle, doc).text, 'Snapshot Policy')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_network_tittle, doc).text, 'Network')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_cpv_vswitch_tittle1, doc).text, 'VPC & VSwitch')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_elastic_ip_addresses_tittle, doc).text, 'Elastic IP Addresses')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_security_tittle, doc).text, 'Security')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_security_group_tittle, doc).text, 'Security Group')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_logon_credential_tittle, doc).text, 'Logon Credential')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_key_pair_tittle, doc).text, 'Key Pair')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_order_setting_tittle, doc).text, 'Order Setting')
        # self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_validity_period_tittle, doc).text, 'Validity Period')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_quantity_tittle, doc).text, 'Quantity')
        # self.check_attribute(self.get_element(UserProductPurchaseLocator.ecs_enable_auto_renewal_tittle, doc).text, 'Enable Auto-renewal')

    # 进入My Resource中的Storage页
    def enter_storage_page(self):
        doc = "进入My Resource中的Storage页"
        time.sleep(3)
        self.wait_elevisble(UserProductPurchaseLocator.security_page, doc)
        self.get_resouce_name_to_click("Storage")
        time.sleep(3)

    # 进入NAS购买页
    def enter_purchase_nas_page(self):
        doc = "进入NAS购买页"
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.nas_createnew_button, doc)
        self.move_mouse_to_element(UserProductPurchaseLocator.nas_createnew_button)
        time.sleep(2)
        self.get_prod_name_to_click('NAS File System')
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.nas_purchase_page, doc)
        # 验证NAS购买页面的相关数据字段
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_region_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_region_btn_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_resource_tittle, doc).text, 'Resource')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_resource_plan_type_btn_tittle, doc).text, 'Resource Plan Type')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_capacity_btn_tittle, doc).text, 'Capacity')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_order_setting_tittle, doc).text, 'Order Setting')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_validity_period_btn_tittle, doc).text, 'Validity Period')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.nas_enable_auto_renewal_btn_tittle, doc).text, 'Enable Auto-renewal')

        # 进入OSS购买页
    def enter_purchase_oss_page(self):
        doc = "进入OSS购买页"
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.oss_createnew_button, doc)
        self.move_mouse_to_element(UserProductPurchaseLocator.oss_createnew_button)
        time.sleep(2)
        self.get_prod_name_to_click('Object Storage Service')
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.oss_purchase_page, doc)
        # 验证OSS购买页面的相关数据字段
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_region_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_region_btn_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_resource_tittle, doc).text, 'Resource')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_resource_plan_type_btn_tittle, doc).text, 'Resource Plan Type')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_capacity_btn_tittle, doc).text, 'Capacity')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_order_setting_tittle, doc).text, 'Order Setting')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_validity_period_btn_tittle, doc).text, 'Validity Period')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.oss_enable_auto_renewal_btn_tittle, doc).text, 'Enable Auto-renewal')

    # 进入My Resource中的Security页
    def enter_security_page(self):
        doc = "进入My Resource中的Security页"
        time.sleep(3)
        self.wait_elevisble(UserProductPurchaseLocator.security_page, doc)
        self.get_resouce_name_to_click("Security")
        time.sleep(3)

    # 进入WAF购买页
    def enter_purchase_waf_page(self):
        doc = "进入WAF购买页"
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.waf_createnew_button, doc)
        self.move_mouse_to_element(UserProductPurchaseLocator.waf_createnew_button)
        time.sleep(2)
        self.get_prod_name_to_click('Web Application Firewall')
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.check_waf_page, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_billing_methon_region_tittle, doc).text, 'Billing Method & Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_billing_methon_tittle, doc).text, 'Billing Method')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_region_tittle, doc).text, 'Region')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_domain_tittle, doc).text, 'Domain')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_domain_btn_tittle, doc).text, 'Domain')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_order_setting_tittle, doc).text,  'Order Setting')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_validity_period_btn_tittle, doc).text,  'Validity Period')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.waf_enable_auto_renewal_btn_tittle, doc).text, 'Enable Auto-renewal')

    # 进入User Management->Role界面
    def enter_user_management_role_page(self):
        time.sleep(2)
        doc = "进入User Management->Role界面"
        for i in range(3):
            try:
                eles = self.get_elements(UserProductPurchaseLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_tab, doc)
                self.click_element(UserProductPurchaseLocator.user_management_tab, doc)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_role_title, doc)
                self.click_element(UserProductPurchaseLocator.user_management_role_title, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_role_page, doc)

    # User Management->Role页面用户字段效验
    def check_role_member_field(self):
        time.sleep(2)
        doc = "User Management->Role页面用户字段效验"
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_name, doc)
        time.sleep(3)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_user_name_title, doc).text, 'User Name')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_account_email_title, doc).text, 'Account Email')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_phone_num_title, doc).text, 'Phone Number')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_creation_time_title, doc).text, 'Creation Time (UTC+08:00)')

    # User Management->Role页面进行搜索功能效验
    def check_role_member_search_func(self):
        time.sleep(2)
        doc = "User Management->Role页面进行搜索功能效验"
        self.keyboard_event(UserProductPurchaseLocator.user_management_role_search_input, doc)
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_search_btn, doc)
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_role_name, doc)
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_name, doc)
        time.sleep(3)
        # 在Role Allocation列表页面机进行搜索
        self.send_keys(UserProductPurchaseLocator.user_management_role_allocation_search_input, '58731', doc)
        # 点击搜索按钮进行搜索操作
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_allocation_search_btn, doc)
        time.sleep(1)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_role_allocation_search_result_data, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_allocation_search_result_data, doc).text, '58731@qq.com')

    # User Management->Role Allocation页面进行用户组列表重置密码效验
    def check_role_allocation_reset_pwd_func(self):
        time.sleep(2)
        doc = "User Management->Role Allocation页面进行用户组列表重置密码效验"
        # 点击reset password按钮
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_btn, doc)
        time.sleep(3)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_box, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_box_tips1, doc).text, 'User must agree with you to help reset password.')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_box_tips2, doc).text, 'New password will be sent to user via selected channel(s).')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_acknowledge_tips, doc).text, 'I acknowledge')
        # 点击勾选I acknowledge按钮，并点击ok
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_acknowledge_btn, doc)
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_ok_btn, doc)
        time.sleep(2)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_succcess_tips, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_management_role_allocation_reset_pwd_succcess_tips, doc).text, 'Reset password successfully.')

    # User Management->Role Allocation页面进行用户组列表菜单功能效验
    def check_role_allocation_manu_func(self):
        time.sleep(2)
        doc = "User Management->Role Allocation页面进行用户组列表重置密码效验"
        # 点击reset password按钮
        self.move_mouse_to_element(UserProductPurchaseLocator.role_allocation_member_info_menu_btn)
        time.sleep(1)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.edit_user_group_btn, doc).text, 'Edit User Group')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.edit_role_btn, doc).text, 'Edit Role')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.lock_btn, doc).text, 'Lock')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.unlock_btn, doc).text, 'Unlock')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.delete_btn, doc).text, 'Delete')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.resend_btn, doc).text, 'Resend')

    # 进入User Management->User页面
    def enter_user_management_user_page(self):
        time.sleep(2)
        doc = "User Management->User页面"
        for i in range(3):
            try:
                eles = self.get_elements(UserProductPurchaseLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_tab, doc)
                self.click_element(UserProductPurchaseLocator.user_management_tab, doc)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_user_title, doc)
                self.click_element(UserProductPurchaseLocator.user_management_user_title, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_user_page, doc)

    # User Management->User页面用户具体信息overview tab页
    def user_member_information_overview_tab(self):
        time.sleep(2)
        doc = "User Management->User页面用户具体信息overview tab页"
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_account_email, doc)
        time.sleep(1)
        self.wait_elevisble(UserProductPurchaseLocator.user_member_specific_info_page, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_basic_info_overview_title, doc).text, 'Basic Information')

    # User Management->User页面用户具体信息User Group tab页
    def user_member_information_user_group_tab(self):
        time.sleep(2)
        doc = "User Management->User页面用户具体信息User Group tab页"
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_member_info_user_group_tab, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_info_user_group_field, doc).text, 'User Group')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_info_members_field, doc).text, 'Members')

    # User Management->User页面用户具体信息Audit tab页
    def user_member_information_audit_tab(self):
        time.sleep(2)
        doc = "User Management->User页面用户具体信息Audit tab页"
        self.move_mouse_to_click_element(UserProductPurchaseLocator.user_member_info_audit_tab, doc)
        time.sleep(3)
        self.wait_elevisble(UserProductPurchaseLocator.user_member_info_audit_account_email_field, doc)
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_info_audit_account_email_field, doc).text, 'Account Email')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_info_audit_event_type_field, doc).text, 'Event Type')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_member_info_audit_remarks_field, doc).text, 'Remarks')

    # 进入User Management->User Group界面
    def enter_user_management_user_group_page(self):
        time.sleep(2)
        doc = "进入User Management->User Group界面"
        for i in range(3):
            try:
                eles = self.get_elements(UserProductPurchaseLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_tab, doc)
                self.click_element(UserProductPurchaseLocator.user_management_tab, doc)
                self.wait_elevisble(UserProductPurchaseLocator.user_management_user_group_title, doc)
                self.click_element(UserProductPurchaseLocator.user_management_user_group_title, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(UserProductPurchaseLocator.user_management_user_group_page, doc)

    # User Management->User Group页面用户字段效验
    def check_user_group_member_field(self):
        time.sleep(2)
        doc = "User Management->User Group页面用户字段效验"
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_account_email, doc).text, 'Account Email')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_status, doc).text, 'Status')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_user_name, doc).text, 'User Name')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_phone_number, doc).text, 'Phone Number')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_creation_time, doc).text, 'Creation Time (UTC+08:00)')
        self.check_attribute(self.get_element(UserProductPurchaseLocator.user_group_actions, doc).text, 'Actions')


